package com.cpecc.controller.extra;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.mail.SendMailThread;
import com.cpecc.mail.SendMailThread1;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.manager.system.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.BaoFeiJLManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BaoFeiSPRManager;
import com.cpecc.manager.wuzi.BuPeiJLManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.CaiGouSPRManager;
import com.cpecc.manager.wuzi.FenTanManager;
import com.cpecc.manager.wuzi.HuanHuoJLManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.PanDianManager;
import com.cpecc.manager.wuzi.ShenQingJLManager;
import com.cpecc.manager.wuzi.ShenQingManager;
import com.cpecc.manager.wuzi.TuiHuoJLManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuJLManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoJLManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Dep;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiJL;
import com.cpecc.model.wuzi.BaoFeiSPR;
import com.cpecc.model.wuzi.BuPei;
import com.cpecc.model.wuzi.BuPeiJL;
import com.cpecc.model.wuzi.CaiGouSPR;
import com.cpecc.model.wuzi.HuanHuo;
import com.cpecc.model.wuzi.HuanHuoJL;
import com.cpecc.model.wuzi.PanDian;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.ShenQingJL;
import com.cpecc.model.wuzi.TuiHuo;
import com.cpecc.model.wuzi.TuiHuoJL;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WeiXiuJL;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;

@Controller
@RequestMapping(value = "/youjian")
public class YouJianController {
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private ShenQingManager shenQingManager;
	
	@Autowired
	private CaiGouSPRManager caiGouSPRManager;
	
	@Autowired
	private ShenQingJLManager shenQingJLManager;
	
	@Autowired
	private RootManager rootManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private WeiXiuJLManager weiXiuJLManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private BaoFeiJLManager baoFeiJLManager;
	
	@Autowired
	private BaoFeiSPRManager baoFeiSPRManager;
	
	@Autowired
	private PanDianManager panDianManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private YiJiaoJLManager yiJiaoJLManager;
	
	@Autowired
	private FenTanManager fenTanManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private BuPeiJLManager buPeiJLManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private TuiHuoJLManager tuiHuoJLManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private HuanHuoJLManager huanHuoJLManager;
	
	@RequestMapping(value = "/youJianDetail")
	public ModelAndView youJianDetail(int id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("youJian", youJianManager.get(id));
		mav.setViewName("youjian/youJianDetail");
		return mav;
	}
	
	@RequestMapping(value = "/youJianEdit")
	public ModelAndView youJianEdit(int id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("youJian", youJianManager.get(id));
		mav.setViewName("youjian/youJianEdit");
		return mav;
	}
	
	@RequestMapping(value = "/youJianSave")
	public void youJianSave(int id,String content,HttpServletResponse response){
		String result="{\"result\":\"success\"}";
		YouJian youJian=youJianManager.get(id);
		youJian.setContent(content);
		youJianManager.cover(youJian);
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/youJianShift")
	public void youJianShift(int id,HttpServletResponse response){
		String result="{\"result\":\"success\"}";
		YouJian youJian=youJianManager.get(id);
		int state=(youJian.getState()+1)%2;
		youJian.setState(state);
		youJianManager.cover(youJian);
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/access")
	public String access(String userId,int huanJie,@RequestParam(value="lx",required=false)Integer lx){
		User user=userManager.getUser(userId);
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
		token.setRememberMe(false);
		currentUser.login(token);
		switch(huanJie){
			case 102:
			case 103:
			case 104:
			case 105:
			case 106:
			case 107:
			case 108:
			case 109: return "redirect:/wuzi/caiGouDaiBan.do";
			case 202: if(lx==1) return "redirect:/wuzi/caiGouDaiBan.do"; else return "redirect:/wuzi/weiXiuDaiBan.do"; 
			case 203: return "redirect:/wuzi/weiXiuDaiBan.do";
			case 302:
			case 303: return "redirect:/wuzi/caiGouDaiBan.do";
			case 402:
			case 403:
			case 404:
			case 405: return "redirect:/wuzi/weiXiuDaiBan.do";
			case 502:
			case 503:
			case 504:
			case 505: return "redirect:/wuzi/yiJiaoDaiBan.do";
			case 602: return "redirect:/wuzi/tuiHuiDaiBan.do";
			case 702:
			case 703:
			case 705:
			case 706:
			case 707:
			case 708:
			case 709:
			case 710:
			case 711:
			case 712: return "redirect:/wuzi/baoFeiDaiBan.do";
			case 802:
			case 803:
			case 804:
			case 805: return "redirect:/wuzi/panDianDaiBan.do";
			default: return "redirect:/login.jsp";
		}
	}
	
	@RequestMapping(value = "/accessPanDian")
	public String accessPanDian(String userId){
		User user=userManager.getUser(userId);
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
		token.setRememberMe(false);
		currentUser.login(token);
		return "redirect:/wuzi/panDianDaiBan.do";
	}
	
	@RequestMapping(value = "/agreePanDian")
	public ModelAndView agreePanDian(String id,String userId){
		ModelAndView mav=new ModelAndView();
		WuZi wuZi=wuZiManager.get(id);
		if(wuZi.getPanDian()!=1){
			mav.setViewName("youjian/overDate");
			return mav;
		}
		User user=userManager.getUser(userId);
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
		token.setRememberMe(false);
		currentUser.login(token);
		PanDian panDian=new PanDian();
		panDian.setFanKuiR(user);
		panDian.setFanKuiRQ(new Date());
		panDian.setFaQiRQ(wuZi.getPanDianRQ());
		panDian.setWuZi(wuZi);
		panDian.setPanDianJieGuo("盘点确认");
		panDianManager.save(panDian);
		wuZi.setPanDian(0);
		wuZiManager.cover(wuZi);
		
		toDoManager.deleteList(wuZi);
		mav.setViewName("youjian/sure");
		return mav;
	}
	
	@RequestMapping(value = "/agree")
	public ModelAndView agree(String id,String userId,int huanJie){
		ModelAndView mav=new ModelAndView();
		User user=userManager.getUser(userId);
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
		token.setRememberMe(false);
		currentUser.login(token);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		if(huanJie==102){
			CaiGouSPR caiGouSPR=caiGouSPRManager.get(id);
			if(caiGouSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			ShenQing shenQing=caiGouSPR.getShenQing();
			if(shenQing.getHuanJie().getId()!=huanJie)
				mav.setViewName("youjian/overDate");
			else{
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setHuanJie(huanJieManager.get(102));
				caiGouSPRManager.delete(id);
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("同意");			
				shenQing.setDangQianShu(shenQing.getDangQianShu()+1);
				toDoManager.delete(shenQing,user);
				if(shenQing.getDangQianShu()-1==shenQing.getZongShu()){
					shenQing.setHuanJie(huanJieManager.get(103));
					//采购部门负责任人选择采买人
					for(User user1:userManager.getCGBMFZR()){
						ToDo toDo=new ToDo();
						toDo.setUser(user1);
						toDo.setHuanJie(huanJieManager.get(103));
						toDo.setCreateDate(new Date());
						toDo.setTask("选择采买人");
						toDo.setType("申请采购");
						toDo.setShenQing(shenQing);
						toDoManager.save(toDo);
						//\\发送邮件
						YouJian youJian=youJianManager.get(103);
						if(youJian!=null && youJian.getState()==1){
							SendMailThread st=new SendMailThread(email, root, 103, user1, youJian);
							st.setShenQing(shenQing);
							Thread t=new Thread(st);
							t.start();
						}
					}
				}
				shenQingManager.cover(shenQing);
				shenQingJL.setUser(user);
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJLManager.save(shenQingJL);
				mav.setViewName("youjian/success");
			}
		}else if(huanJie==107){
			ShenQing shenQing=shenQingManager.get(id);
			if(shenQing.getHuanJie().getId()!=huanJie)
				mav.setViewName("youjian/overDate");
			else{
				shenQing.setHuanJie(huanJieManager.get(108));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(107));
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("使用人验收");		
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				WuZi wuZi=shenQing.getWuZi();
				wuZi.setChuKuRQ(new Date());
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);
				toDoManager.deleteList(shenQing);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo = new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(108));
					toDo.setShenQing(shenQing);
					toDo.setTask("使用人已验收");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(108);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 108, user1, youJian);
						st.setWuZi(wuZi);
						Thread t=new Thread(st);
						t.start();
					}
				}
				mav.setViewName("youjian/success");
			}
		}else if(huanJie==202){
			HuanHuo huanHuo=huanHuoManager.get(id);
			if(huanHuo.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			if(huanHuo.getShenQing()!=null){
				ShenQing shenQing=huanHuo.getShenQing();
				shenQing.setHuanJie(huanJieManager.get(203));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(202));
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("同意");
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				huanHuo.setYuanChanPinXLH(shenQing.getWuZi().getChanPinXLH());
				huanHuo.setHuanJie(huanJieManager.get(203));
				huanHuoManager.cover(huanHuo);
				HuanHuoJL huanHuoJL=new HuanHuoJL();
				huanHuoJL.setCaoZuoRQ(new Date());
				huanHuoJL.setHuanHuo(huanHuo);
				huanHuoJL.setHuanJie(huanJieManager.get(202));
				huanHuoJL.setUser(user);
				huanHuoJL.setPiFuLX("采买人同意换货");
				huanHuoJL.setPiFuNR("同意");
				huanHuoJLManager.save(huanHuoJL);
				toDoManager.delete(shenQing, user);
				//固资管理员发放
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setUser(user1);
					toDo.setHuanJie(huanJieManager.get(111));
					toDo.setCreateDate(new Date());
					toDo.setTask("物资发放");
					toDo.setType("申请采购");
					toDo.setShenQing(shenQing);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(105);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 105, user1, youJian);
						st.setShenQing(shenQing);
						st.setWuZi(shenQing.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
			}else if(huanHuo.getWeiXiu()!=null){
				WeiXiu weiXiu=huanHuo.getWeiXiu();
				weiXiu.setHuanJie(huanJieManager.get(204));
				weiXiuManager.cover(weiXiu);
				WeiXiuJL weiXiuJL=new WeiXiuJL();
				weiXiuJL.setCaoZuoRQ(new Date());
				weiXiuJL.setHuanJie(huanJieManager.get(202));
				weiXiuJL.setPiFuLX("采买人同意换货");
				weiXiuJL.setPiFuNR("同意");
				weiXiuJL.setUser(user);
				weiXiuJLManager.save(weiXiuJL);
				huanHuo.setYuanChanPinXLH(weiXiu.getWuZi().getChanPinXLH());		
				huanHuo.setHuanJie(huanJieManager.get(204));
				huanHuoManager.cover(huanHuo);
				HuanHuoJL huanHuoJL=new HuanHuoJL();
				huanHuoJL.setCaoZuoRQ(new Date());
				huanHuoJL.setHuanHuo(huanHuo);
				huanHuoJL.setHuanJie(huanJieManager.get(202));
				huanHuoJL.setUser(user);
				huanHuoJL.setPiFuLX("采买人同意换货");
				huanHuoJL.setPiFuNR("同意");
				huanHuoJLManager.save(huanHuoJL);
				toDoManager.deleteList(weiXiu);
				//\\发送邮件
				for(User user1:userManager.getGZGLY()){
					YouJian youJian=youJianManager.get(203);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 203, user1, youJian);
						st.setWuZi(huanHuo.getWuZi());
						st.setHuanHuo(huanHuo);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==203){
			
		}else if(huanJie==302){
			TuiHuo tuiHuo=tuiHuoManager.get(id);
			if(tuiHuo.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			ShenQing shenQing=tuiHuo.getShenQing();
			WuZi wuZi=tuiHuo.getWuZi();		
			shenQing.setHuanJie(huanJieManager.get(303));		
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJL.setHuanJie(huanJieManager.get(302));		
			shenQingJL.setPiFuLX("采买人同意退货");
			shenQingJL.setPiFuNR("同意");
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
			tuiHuo.setHuanJie(huanJieManager.get(303));
			tuiHuoManager.cover(tuiHuo);
			TuiHuoJL tuiHuoJL=new TuiHuoJL();
			tuiHuoJL.setCaoZuoRQ(new Date());
			tuiHuoJL.setTuiHuo(tuiHuo);
			tuiHuoJL.setHuanJie(huanJieManager.get(302));
			tuiHuoJL.setPiFuLX("采买人同意退货");
			tuiHuoJL.setPiFuNR("同意");
			tuiHuoJL.setUser(user);
			tuiHuoJLManager.save(tuiHuoJL);
			wuZi.setShiYongZT(shiYongZTManager.get(104));
			wuZiManager.cover(wuZi);
			toDoManager.deleteList(shenQing);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo = new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(303));
				toDo.setShenQing(shenQing);
				toDo.setTask("退货确认");
				toDo.setType("申请采购");
				toDo.setUser(user1);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(303);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 303, user1, youJian);
					st.setWuZi(wuZi);
					st.setTuiHuo(tuiHuo);
					Thread t=new Thread(st);
					t.start();
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==303){
			TuiHuo tuiHuo=tuiHuoManager.get(id);
			if(tuiHuo.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			tuiHuo.setHuanJie(huanJieManager.get(304));
			tuiHuoManager.cover(tuiHuo);
			TuiHuoJL tuiHuoJL=new TuiHuoJL();
			tuiHuoJL.setCaoZuoRQ(new Date());
			tuiHuoJL.setHuanJie(huanJieManager.get(303));
			tuiHuoJL.setPiFuLX("固资管理员确认");
			tuiHuoJL.setPiFuNR("同意");
			tuiHuoJL.setTuiHuo(tuiHuo);
			tuiHuoJL.setUser(user);
			tuiHuoJLManager.save(tuiHuoJL);
			ShenQing shenQing=tuiHuo.getShenQing();
			shenQing.setHuanJie(huanJieManager.get(304));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJL.setHuanJie(huanJieManager.get(303));
			shenQingJL.setPiFuLX("固资管理员确认");
			shenQingJL.setPiFuNR("同意");
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
			toDoManager.deleteList(shenQing);
			mav.setViewName("youjian/success");
		}else if(huanJie==404){
			WeiXiu weiXiu=weiXiuManager.get(id);
			if(weiXiu.getHuanJie().getId()!=huanJie)
				mav.setViewName("youjian/overDate");
			else{
				weiXiu.setHuanJie(huanJieManager.get(405));
				weiXiuManager.cover(weiXiu);		
				WeiXiuJL weiXiuJL=new WeiXiuJL();
				weiXiuJL.setCaoZuoRQ(new Date());
				weiXiuJL.setHuanJie(huanJieManager.get(404));
				weiXiuJL.setPiFuLX("使用人维修确认");
				weiXiuJL.setPiFuNR("同意");
				weiXiuJL.setUser(user);
				weiXiuJL.setWeiXiu(weiXiu);
				weiXiuJLManager.save(weiXiuJL);
				WuZi wuZi=weiXiu.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);
				//待维修入账
				toDoManager.deleteList(weiXiu);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(405));
					toDo.setTask("维修入账");
					toDo.setType("维修");
					toDo.setUser(user1);
					toDo.setWeiXiu(weiXiu);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(405);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 405, user1, youJian);
						st.setWeiXiu(weiXiu);
						st.setWuZi(weiXiu.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
				mav.setViewName("youjian/success");
			}
		}else if(huanJie==502){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			yiJiao.setHuanJie(huanJieManager.get(503));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(502));
			yiJiaoJL.setPiFuLX("移交入库");
			yiJiaoJL.setPiFuNR("接收");
			yiJiaoJL.setUser(user);
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJLManager.save(yiJiaoJL);
			WuZi wuZi=yiJiao.getWuZi();
			fenTanManager.quChuFenTan(wuZi, new Date());
			wuZi.setShiYongZT(shiYongZTManager.get(105));
			wuZi.setUser(user);
			wuZi.setDep(user.getDep());		
			wuZi.setGuanLiBM(user.getDep());
			wuZiManager.cover(wuZi);
			
			toDoManager.deleteList(yiJiao);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(503));
				toDo.setTask("移交发放");
				toDo.setType("移交");
				toDo.setUser(user1);
				toDo.setYiJiao(yiJiao);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(503);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 503, user1, youJian);
					st.setYiJiao(yiJiao);
					st.setWuZi(wuZi);
					Thread t=new Thread(st);
					t.start();
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==504){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			yiJiao.setHuanJie(huanJieManager.get(505));
			yiJiaoManager.cover(yiJiao);
			WuZi wuZi=yiJiao.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(102));
			wuZiManager.cover(wuZi);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(504));
			yiJiaoJL.setPiFuLX("新使用人接收");
			yiJiaoJL.setPiFuNR("接收");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			if(wuZi.getPanDian()==3){
				wuZi.setPanDian(1);
				wuZiManager.cover(wuZi);
			}
			toDoManager.deleteList(yiJiao);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(505));
				toDo.setTask("移交结果确认");
				toDo.setType("移交");
				toDo.setUser(user1);
				toDo.setYiJiao(yiJiao);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(505);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 505, user1, youJian);
					st.setYiJiao(yiJiao);
					st.setWuZi(wuZi);
					Thread t=new Thread(st);
					t.start();
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==505){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			yiJiao.setHuanJie(huanJieManager.get(506));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(505));
			yiJiaoJL.setPiFuLX("移交确认");
			yiJiaoJL.setPiFuNR("移交确认");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			toDoManager.deleteList(yiJiao);
			mav.setViewName("youjian/success");
		}else if(huanJie==602){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			Dep dep=userManager.getUserDep(user);
			yiJiao.setHuanJie(huanJieManager.get(603));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(602));
			yiJiaoJL.setPiFuLX("固资管理员接收");
			yiJiaoJL.setPiFuNR("同意");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			WuZi wuZi=yiJiao.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(105));
			wuZi.setGuanLiBM(dep);
			wuZi.setDep(dep);
			wuZi.setUser(user);
			wuZiManager.cover(wuZi);
			fenTanManager.quChuFenTan(wuZi, new Date());
			toDoManager.deleteList(yiJiao);
			mav.setViewName("youjian/success");
		}else if(huanJie==702){
			BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
			if(baoFeiSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			BaoFei baoFei=baoFeiSPR.getBaoFei();
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			baoFei.setDangQianS(baoFei.getDangQianS()+1);
			baoFeiSPRManager.delete(id);
			toDoManager.delete(baoFei, user);
			if(baoFei.getDangQianS()-1==baoFei.getZongRS()){
				baoFei.setHuanJie(huanJieManager.get(703));
				baoFei.setZongRS(0);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(703));
					toDo.setTask("固资管理员审批");
					toDo.setType("报废");
					toDo.setUser(user1);
					toDo.setBaoFei(baoFei);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(703);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 703, user1, youJian);
						st.setBaoFei(baoFei);
						st.setWuZi(baoFei.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
				SendMailThread1 st1=new SendMailThread1(email, baoFei.getUser(), youJianManager.get(2), baoFei.getWuZi());
				Thread t1=new Thread(st1);
				t1.start();
			}
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setHuanJie(huanJieManager.get(702));
			baoFeiJL.setPiFuLX("通过");
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			mav.setViewName("youjian/success");
		}else if(huanJie==703){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			Dep dep=user.getDep();
			baoFei.setHuanJie(huanJieManager.get(704));
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setHuanJie(huanJieManager.get(703));
			baoFeiJL.setPiFuLX("通过");
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			WuZi wuZi=baoFei.getWuZi();
			wuZi.setGuanLiBM(dep);
			wuZi.setDep(dep);
			wuZi.setUser(user);
			wuZi.setShiYongZT(shiYongZTManager.get(106));
			wuZiManager.cover(wuZi);
			mav.setViewName("youjian/success");
		}else if(huanJie==705){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			baoFei.setHuanJie(huanJieManager.get(706));
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setHuanJie(huanJieManager.get(705));
			baoFeiJL.setPiFuLX("主管初次审批同意");
			baoFeiJL.setPiFuNR("同意报废");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			toDoManager.deleteList(baoFei);
			mav.setViewName("youjian/success");
		}else if(huanJie==707){
			BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
			if(baoFeiSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			BaoFei baoFei=baoFeiSPR.getBaoFei();
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			int dangQianShu=baoFei.getDangQianS();
			baoFei.setDangQianS(dangQianShu+1);
			baoFeiSPRManager.delete(id);
			toDoManager.delete(baoFei, baoFeiSPR.getUser());
			if(baoFei.getDangQianS()-1==baoFei.getZongRS()){
				baoFei.setHuanJie(huanJieManager.get(708));
				baoFei.setZongRS(0);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(708));
					toDo.setBaoFei(baoFei);
					toDo.setTask("报废物资处理");
					toDo.setType("报废");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(708);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 708, user1, youJian);
						st.setBaoFei(baoFei);
						st.setBaoFeiJL(baoFeiJLManager.last(baoFei));
						st.setWuZi(baoFei.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setHuanJie(huanJieManager.get(707));
			baoFeiJL.setPiFuLX("会签通过");
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			mav.setViewName("youjian/success");
		}else if(huanJie==709){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			if(baoFei.getChuLiFS().equals("内购")){
				baoFei.setHuanJie(huanJieManager.get(710));
			}else{
				baoFei.setHuanJie(huanJieManager.get(711));
			}
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setHuanJie(huanJieManager.get(709));
			baoFeiJL.setPiFuLX("报废处理审批");
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			toDoManager.deleteList(baoFei);
			if(baoFei.getChuLiFS().equals("内购")){
				ToDo toDo=new ToDo();
				toDo.setBaoFei(baoFei);
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(710));
				toDo.setTask("内购确认");
				toDo.setType("报废");
				toDo.setUser(baoFei.getUser());
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(710);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 710, baoFei.getUser(), youJian);
					st.setBaoFei(baoFei);
					st.setBaoFeiJL(baoFeiJLManager.last(baoFei));
					st.setWuZi(baoFei.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}else{
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setBaoFei(baoFei);
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(711));
					toDo.setTask("打印报废单据");
					toDo.setType("报废");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(711);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 711, user1, youJian);
						st.setBaoFei(baoFei);
						st.setBaoFeiJL(baoFeiJLManager.last(baoFei));
						st.setWuZi(baoFei.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==710){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			baoFei.setHuanJie(huanJieManager.get(711));
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setHuanJie(huanJieManager.get(710));
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setPiFuLX("使用人购买");
			baoFeiJL.setPiFuNR("使用人同意购买");
			baoFeiJL.setUser(user);
			baoFeiJLManager.save(baoFeiJL);
			toDoManager.deleteList(baoFei);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setBaoFei(baoFei);
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(711));
				toDo.setTask("打印报废单据");
				toDo.setType("报废");
				toDo.setUser(user1);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(711);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 711, user1, youJian);
					st.setBaoFei(baoFei);
					st.setBaoFeiJL(baoFeiJLManager.last(baoFei));
					st.setWuZi(baoFei.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==712){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			baoFei.setHuanJie(huanJieManager.get(713));
			baoFeiManager.cover(baoFei);
			BaoFeiJL baoFeiJL=new BaoFeiJL();
			baoFeiJL.setBaoFei(baoFei);
			baoFeiJL.setCaoZuoRQ(new Date());
			baoFeiJL.setUser(user);
			baoFeiJL.setPiFuNR("同意");
			baoFeiJL.setPiFuLX("通过");
			baoFeiJL.setHuanJie(huanJieManager.get(712));
			baoFeiJLManager.save(baoFeiJL);
			
			toDoManager.deleteList(baoFei);
			mav.setViewName("youjian/success");
		}else if(huanJie==804){
			BuPei buPei=buPeiManager.get(id);
			if(buPei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			buPei.setHuanJie(huanJieManager.get(805));
			buPei.setWanChengRQ(new Date());
			buPeiManager.cover(buPei);
			BuPeiJL buPeiJL=new BuPeiJL();
			buPeiJL.setBuPei(buPei);
			buPeiJL.setCaoZuoRQ(new Date());
			buPeiJL.setHuanJie(huanJieManager.get(804));
			buPeiJL.setPiFuLX("赔偿确认");
			buPeiJL.setPiFuNR("确认");
			buPeiJL.setUser(user);
			buPeiJLManager.save(buPeiJL);
			WuZi wuZi=buPei.getWuZi();
			wuZi.setPanDian(4);
			wuZi.setShiYongZT(shiYongZTManager.get(110));
			wuZiManager.cover(wuZi);
			toDoManager.deleteList(buPei.getWuZi());
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(805));
				toDo.setTask("赔偿确认");
				toDo.setType("赔偿");
				toDo.setUser(user1);
				toDo.setWuZi(buPei.getWuZi());
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(805);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 805, user1, youJian);
					st.setWuZi(buPei.getWuZi());
					st.setBuPei(buPei);
					Thread t=new Thread(st);
					t.start();
				}
			}
			mav.setViewName("youjian/success");
		}else if(huanJie==805){
			BuPei buPei=buPeiManager.get(id);
			if(buPei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			buPei.setHuanJie(huanJieManager.get(806));
			buPei.setWanChengRQ(new Date());
			buPeiManager.cover(buPei);
			BuPeiJL buPeiJL=new BuPeiJL();
			buPeiJL.setBuPei(buPei);
			buPeiJL.setCaoZuoRQ(new Date());
			buPeiJL.setHuanJie(huanJieManager.get(805));
			buPeiJL.setPiFuLX("赔偿记账");
			buPeiJL.setPiFuNR("入账");
			buPeiJL.setUser(user);
			buPeiJLManager.save(buPeiJL);
			WuZi wuZi=buPei.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(110));
			wuZiManager.cover(wuZi);
			toDoManager.deleteList(buPei.getWuZi());
			mav.setViewName("youjian/success");
		}
		return mav;
	}
	
	@RequestMapping(value = "/disagree")
	public ModelAndView disagree(String id,String userId,int huanJie){
		User user=userManager.getUser(userId);
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
		token.setRememberMe(false);
		currentUser.login(token);
		ModelAndView mav=new ModelAndView();
		if(huanJie==102){
			CaiGouSPR caiGouSPR=caiGouSPRManager.get(id);
			if(caiGouSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			ShenQing shenQing=caiGouSPR.getShenQing();
			if(shenQing.getHuanJie().getId()!=huanJie)
				mav.setViewName("youjian/overDate");
			else{
				mav.addObject("caiGouSPRId", id);
				mav.setViewName("shenqing/buMenCGBTY");
			}
		}else if(huanJie==202){
			HuanHuo huanHuo=huanHuoManager.get(id);
			if(huanHuo==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			if(huanHuo.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			if(huanHuo.getShenQing()!=null){
				mav.addObject("hhId", id);
				mav.setViewName("shenqing/cghhBTY");
			}else if(huanHuo.getWeiXiu()!=null){
				mav.addObject("wxhhId", id);
				mav.setViewName("weixiu/wxhhBTY");
			}
		}else if(huanJie==302){
			TuiHuo tuiHuo=tuiHuoManager.get(id);
			if(tuiHuo==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			if(tuiHuo.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
			}else{
				mav.addObject("thId", id);
				mav.setViewName("shenqing/cgthBTY");
			}
		}else if(huanJie==502){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("tuiHuiId", id);
			mav.setViewName("tuihui/tuiHuiBoHui");
		}else if(huanJie==504){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("yiJiaoId", id);
			mav.setViewName("yijiao/yiJiaoTuiHui");
		}else if(huanJie==602){
			YiJiao yiJiao=yiJiaoManager.get(id);
			if(yiJiao.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("tuiHuiId", id);
			mav.setViewName("tuihui/tuiHuiBoHui");
		}else if(huanJie==702){
			BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
			if(baoFeiSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			BaoFei baoFei=baoFeiSPR.getBaoFei();
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
			}else{
				mav.addObject("baoFeiId", id);
				mav.setViewName("baofei/baoFeiBTY");
			}
		}else if(huanJie==703){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("baoFeiId", id);
			mav.setViewName("baofei/baoFeiGZGLYBTY");
		}else if(huanJie==705){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("zgspId", id);
			mav.setViewName("baofei/zgspBTY");
		}else if(huanJie==707){
			BaoFeiSPR baoFeiSPR=baoFeiSPRManager.get(id);
			if(baoFeiSPR==null){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			BaoFei baoFei=baoFeiSPR.getBaoFei();
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("baoFeiId", id);
			mav.setViewName("baofei/huiQianBTY");
		}else if(huanJie==709){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("bfclspId", id);
			mav.setViewName("baofei/bfclspBTY");
		}else if(huanJie==712){
			BaoFei baoFei=baoFeiManager.get(id);
			if(baoFei.getHuanJie().getId()!=huanJie){
				mav.setViewName("youjian/overDate");
				return mav;
			}
			mav.addObject("baoFeiId", id);
			mav.setViewName("baofei/chuLiBTY");
		}
		return mav;
	}
}
